﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using RapChieuPhim.DTO;
using System.Data;
using System.Collections;

namespace RapChieuPhim.DAO
{
    class NguoiDatDAO
    {
        public NguoiDatDTO GetByID(string id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaNguoiDat", id);
            DataTable dt = cn.SqlExcuteQuery("LayNguoiDatTheoID", param);
            NguoiDatDTO NguoiDat = new NguoiDatDTO();

            if (dt.Rows.Count!=0)
            {
                NguoiDat.MaNguoiDat = dt.Rows[0]["MaNguoiDat"].ToString();
                NguoiDat.HoTen = dt.Rows[0]["HoTen"].ToString();
                NguoiDat.SoDienThoai = dt.Rows[0]["SoDienThoai"].ToString();
            }
            return NguoiDat;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllNguoiDat");

            if (dt.Rows.Count != 0)
            {    
                foreach (DataRow dr in dt.Rows)
                {
                    NguoiDatDTO NguoiDat = new NguoiDatDTO();
                    NguoiDat.MaNguoiDat = dr["MaNguoiDat"].ToString();
                    NguoiDat.HoTen = dr["HoTen"].ToString();
                    NguoiDat.SoDienThoai = dr["SoDienThoai"].ToString();
                    array.Add(NguoiDat);
                }
            }
            return array;
        }

        public void Insert(NguoiDatDTO NguoiDat)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaNguoiDat", NguoiDat.MaNguoiDat);
            param[1] = new SqlParameter("@HoTen", NguoiDat.HoTen);
            param[2] = new SqlParameter("@SoDienThoai", NguoiDat.SoDienThoai);
            cn.SqlExcuteNonQuery("InsertNguoiDat", param);
        }

        public void Update(NguoiDatDTO NguoiDat)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaNguoiDat", NguoiDat.MaNguoiDat);
            param[1] = new SqlParameter("@HoTen", NguoiDat.HoTen);
            param[2] = new SqlParameter("@SoDienThoai", NguoiDat.SoDienThoai);
            cn.SqlExcuteNonQuery("UpdateNguoiDat", param);
        }

        public void Delete(string id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaNguoiDat", id);
            cn.SqlExcuteNonQuery("DeleteNguoiDat", param);
        }
    }
}
